package com.sky.loveshop.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.loveshop.entity.Sku;
import com.sky.loveshop.entity.StockChangeHistory;
import com.sky.loveshop.entity.User;
import com.sky.loveshop.entity.exception.BizException;
import com.sky.loveshop.entity.qo.StockChangeHistoryQO;
import com.sky.loveshop.entity.vo.SkuVO;
import com.sky.loveshop.entity.vo.StockChangeHistoryVO;
import com.sky.loveshop.entity.vo.UserVO;
import com.sky.loveshop.mapper.StockChangeHistoryMapper;
import com.sky.loveshop.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author xcy
 * @version V1.0
 * @description
 * @date 2024/8/18 0:06
 */
@Slf4j
@Service
public class StockChangeHistoryServiceImpl implements StockChangeHistoryService {

    @Autowired
    private StockChangeHistoryMapper stockChangeHistoryMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private SkuService skuService;

    @Override
    public List<StockChangeHistoryVO> listStockChangeHistory(StockChangeHistoryQO qo) {
        log.info("listStockChangeHistoryParam = {}", JSONUtil.toJsonStr(qo));
        return stockChangeHistoryMapper.searchAll(qo);
    }

    @Override
    public PageInfo<StockChangeHistoryVO> listStockChangeHistoryPages(StockChangeHistoryQO qo) {
        log.info("listStockChangeHistoryPagesParam = {}", JSONUtil.toJsonStr(qo));
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List<StockChangeHistoryVO> stockChangeHistoryList = stockChangeHistoryMapper.searchAll(qo);
        if (CollectionUtil.isEmpty(stockChangeHistoryList)) {
            return new PageInfo<>();
        }
        List<Integer> userIds = stockChangeHistoryList.stream().map(StockChangeHistory::getUserId).distinct().collect(Collectors.toList());
        List<Integer> skuIds = stockChangeHistoryList.stream().map(StockChangeHistory::getSkuId).distinct().collect(Collectors.toList());
        List<SkuVO> skuList = skuService.getSkuListByIds(skuIds);

        Map<Integer, String> skuIdShopNameMap = skuList.stream().collect(Collectors.toMap(Sku::getId, SkuVO::getShopName));
        Map<Integer, String> skuIdProductNameMap = skuList.stream().collect(Collectors.toMap(Sku::getId, SkuVO::getProductName));

        List<UserVO> userList = userService.getUserListByIds(userIds);
        Map<Integer, String> userIdNameMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(userList)) {
            userIdNameMap = userList.stream().collect(Collectors.toMap(User::getId, User::getUserName));
        }
        userIdNameMap.put(9999, "门店端(骑手核销/线下购买)");

        Map<Integer, String> finalUserIdNameMap = userIdNameMap;
        stockChangeHistoryList.stream().forEach(s -> {
            s.setUserName(finalUserIdNameMap.get(s.getUserId()));
            s.setShopName(skuIdShopNameMap.get(s.getSkuId()));
            s.setProductName(skuIdProductNameMap.get(s.getSkuId()));
        });
        return new PageInfo<>(stockChangeHistoryList);
    }

    @Override
    public StockChangeHistoryVO getStockChangeHistoryById(Integer id) throws BizException {
        StockChangeHistory stockChangeHistory = stockChangeHistoryMapper.getStockChangeHistoryById(id);
        StockChangeHistoryVO vo = new StockChangeHistoryVO();
        BeanUtils.copyProperties(stockChangeHistory, vo);
        Integer userId = vo.getUserId();
        Integer skuId = vo.getSkuId();
        UserVO user = userService.getUserById(userId);
        SkuVO sku = skuService.getSkuById(skuId);
        vo.setProductName(sku.getProductName());
        vo.setUserName(user.getUserName());
        vo.setShopName(sku.getShopName());
        return vo;
    }


    @Override
    public List<StockChangeHistoryVO> getStockChangeHistoryListByIds(List<Integer> ids) {
        List<StockChangeHistory> stockChangeHistoryList = stockChangeHistoryMapper.getStockChangeHistoryListByIds(ids);

        ArrayList<StockChangeHistoryVO> stockChangeHistoryVOList = new ArrayList<>();

        stockChangeHistoryList.stream().forEach(s -> {
            StockChangeHistoryVO stockChangeHistoryVO = new StockChangeHistoryVO();
            stockChangeHistoryVOList.add(stockChangeHistoryVO);
        });
        return stockChangeHistoryVOList;
    }

    @Override
    public Integer addStockChangeHistory(StockChangeHistory stockChangeHistory) {
        stockChangeHistory.setCreateTime(new Date());
        return stockChangeHistoryMapper.insertOne(stockChangeHistory);
    }
}
